package com.lizhijun.service.impl;

import com.lizhijun.mapper.EmployeeMapper;
import com.lizhijun.pojo.Employee;
import com.lizhijun.service.EmployeeService;
import com.lizhijun.utils.MybatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("employeeService")
public class EmployeeServiceImpl implements EmployeeService {

    private SqlSessionFactory sqlSessionFactory = MybatisUtil.getSqlSessionFactory();

    @Override
    public List<Employee> findAbsence() {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class);
        List<Employee> employeeList = employeeMapper.findAbsence();
        sqlSession.close();
        return employeeList;
    }

    @Override
    public void update(Employee employee){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class);
        employeeMapper.update(employee);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public void delete(Employee employee){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class);
        employeeMapper.delete(employee);
        sqlSession.commit();
        sqlSession.close();
    }

    public List<Employee> findBonus(){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class);
        List<Employee> employeeList = employeeMapper.findBonus();
        sqlSession.close();
        return employeeList;
    }

    @Override
    public void updateBonus(Employee employee) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class);
        employeeMapper.updateBonus(employee);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public void updateDeduction(Employee employee) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class);
        employeeMapper.updateDeduction(employee);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public List<Employee> findDepartment() {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
        List<Employee> employeeList = mapper.findDepartment();
        sqlSession.close();
        return employeeList;
    }

    @Override
    public void insertEmployee(Employee employee) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
        mapper.insertEmployee(employee);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public List<Employee> findStatus() {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
        List<Employee> employeeList = mapper.findStatus();
        sqlSession.close();
        return employeeList;
    }

    @Override
    public void updateStatus(Employee employee) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class);
        employeeMapper.updateStatus(employee);
        sqlSession.commit();
        sqlSession.close();
    }
}
